package com.example.study.commen.print;

import javax.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.Sides;
import java.awt.print.PrinterJob;
import java.io.*;
import java.util.ArrayList;
import java.util.List;


public class DrivePrintService {
    // 传入文件和打印机名称
    public void print(File[] files, String printerName) throws PrintException, IOException {
        if (null == files || files.length <= 0) {
            System.out.println("缺少打印文件11");
            throw new NullPointerException("缺少打印文件11");
        }
        List<InputStream> streams = fileStreams(files);
//        if (CollUtil.isEmpty(streams)) {
//            log.warn("缺少打印文件");
//            throw new NullPointerException("缺少打印文件");
//        }

        DocFlavor flavor = getDocFlavor();
        PrintRequestAttributeSet aset = getPrintRequestAttribute();

        for (InputStream in : streams) {
            DocPrintJob docPrintJob = getPrintJob(printerName);
            docPrintJob.print(new SimpleDoc(in, flavor, null), aset);
        }
    }

    private List<InputStream> fileStreams(File[] files) {
        if (null == files || files.length <= 0) {
            System.out.println("缺少打印文件");
            throw new NullPointerException("缺少打印文件");
        }
//        if (ArrayUtil.isEmpty(files)) {
//            System.out.println("缺少打印文件");
//            throw new NullPointerException("缺少打印文件");
//        }
        List<InputStream> ins = new ArrayList<>();
        try {
            for (File file : files) {
                ins.add(new FileInputStream(file));
            }
        } catch (FileNotFoundException e) {
            System.out.println("打印文件缺失：{}" + e.getMessage());
        }
        return ins;
    }

    private DocFlavor getDocFlavor() {
        // 设置打印格式，如果未确定类型，可选择autosense
        return DocFlavor.INPUT_STREAM.AUTOSENSE;
//        return DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8;
    }

    private PrintRequestAttributeSet getPrintRequestAttribute() {
        // 设置打印参数
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(new Copies(1)); //份数
//        aset.add(MediaSize.ISO.A4); //纸张
        // aset.add(Finishings.STAPLE);//装订
        aset.add(Sides.ONE_SIDED);//单双面
        return aset;
    }

    private DocPrintJob getPrintJob(String printerName) {
        if (printerName == null) {
            System.out.println("请选择打印机");
            throw new NullPointerException("请选择打印机");
        }
        //获得本台电脑连接的所有打印机
        PrintService[] printServices = PrinterJob.lookupPrintServices();
        if (printServices == null || printServices.length == 0) {
            System.out.println("打印失败，未找到可用打印机，请检查。");
            throw new NullPointerException("打印失败，未找到可用打印机，请检查。");
        }
        //匹配指定打印机
        PrintService printService = null;
        for (PrintService service : printServices) {
            if (printerName.equals(service.getName())) {
                printService = service;
                break;
            }
        }
        if (printService == null) {
//            String format = StrUtil.format("打印失败，未找到名称为 [{}] 的打印机，请检查。", printerName);
//            log.warn(format);
            System.out.println("打印失败，未找到名称为 [{}] 的打印机");
            throw new NullPointerException("打印失败，未找到名称为 [{}] 的打印机");
        }
        return printService.createPrintJob();
    }
}